//
//  KKWebServiceManager.m
//  上海杨浦
//
//  Created by leo on 2017/5/11.
//  Copyright © 2017年 FrankChen. All rights reserved.
//

#import "KKWebServiceManager.h"

#import "ServiceHelper.h"

#import "GDataXMLNode.h"

static NSString *ServiceURL = @"http://116.228.162.163:8080/xxgk/Webservice/InfoQuery.asmx";

@implementation KKWebServiceManager

+ (void)kk_getModuleId:(NSString *)moduleId pageIndex:(NSInteger)pageIndex success:(KKRequestSuccessHandle)success faile:(KKRequestFaileHandle)faile {
    
    NSMutableArray *params=[NSMutableArray array];
    NSString *page=[NSString stringWithFormat:@"%@",@(pageIndex)];
    
    [params addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"20",@"PageSize", nil]];
    [params addObject:[NSDictionary dictionaryWithObjectsAndKeys:page,@"CurrentPageIndex", nil]];
    
    [params addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"",@"OuCode", nil]];
    [params addObject:[NSDictionary dictionaryWithObjectsAndKeys: moduleId,@"ModuleId", nil]];
    [params addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"",@"DateFrom", nil]];
    [params addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"",@"DateTo", nil]];
    
    
    ServiceArgs *args1=[[[ServiceArgs alloc] init] autorelease];
    args1.serviceURL= ServiceURL;
    
    args1.serviceNameSpace=@"http://tempuri.org/";
    args1.methodName=@"GetInfoList";
    args1.soapParams=params;
    
    //NSLog(@"soap=%@\n",args1.soapMessage);
    ServiceResult *result=[ServiceHelper syncService:args1];
    
    //NSLog(@"%@",[result.request responseString]);
    
    NSString *xml=[[result.request responseString] stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"];
    xml=[xml stringByReplacingOccurrencesOfString:@"&gt;" withString:@">"];
    
    if(xml.length){
        // 利用正则表达式取出<return></return>之间的字符串
        NSRegularExpression *regular = [[NSRegularExpression alloc] initWithPattern:@"(?<=GetInfoListResult\\>).*(?=</GetInfoListResult)" options:NSRegularExpressionCaseInsensitive error:nil];
        for (NSTextCheckingResult *checkingResult in [regular matchesInString:xml options:0 range:NSMakeRange(0, xml.length)]) {
            // 得到返回的数据信息
            NSString *response_result = [xml substringWithRange:checkingResult.range];
            
            GDataXMLDocument *xmlDocument = [[GDataXMLDocument alloc] initWithXMLString:response_result error:nil];
            GDataXMLElement *xmlEle = [xmlDocument rootElement];
            
            NSArray *elements = [xmlEle elementsForName: @"Information"];
            GDataXMLElement *infoElement = [elements firstObject];
            NSArray *infoSubElements = [infoElement elementsForName: @"Info"];
            
            NSMutableArray *arr_infoItem = [NSMutableArray array];
            for (GDataXMLElement *subElement in infoSubElements) {
                
                KKInfoItem *infoItem = [KKInfoItem new];
                
                GDataXMLElement *titleElement = [[subElement elementsForName: @"Title"] firstObject];
                infoItem.kk_title = [titleElement stringValue];
                
                GDataXMLElement *InfoDateElement = [[subElement elementsForName: @"InfoDate"] firstObject];
                infoItem.kk_InfoDate = [InfoDateElement stringValue];
                
                GDataXMLElement *InfoidElement = [[subElement elementsForName: @"Infoid"] firstObject];
                infoItem.kk_Infoid = [InfoidElement stringValue];
                
                GDataXMLElement *CategoryNameElement = [[subElement elementsForName: @"CategoryName"] firstObject];
                infoItem.kk_CategoryName = [CategoryNameElement stringValue];
                
                [arr_infoItem addObject: infoItem];
            }
            
            if (arr_infoItem.count) {
                if (success) {
                    success(arr_infoItem);
                }
            }
        }
    }
    else {
        if (faile) {
            faile();
        }
    }
}

+ (void)kk_getInfoDetailWithInfoId:(NSString*)infoId success:(KKRequestSuccessHandle)success faile:(KKRequestFaileHandle)faile {
    
    NSMutableArray *params=[NSMutableArray array];
    
    
    [params addObject:[NSDictionary dictionaryWithObjectsAndKeys:infoId,@"InfoID", nil]];
    
    
    ServiceArgs *args1=[[[ServiceArgs alloc] init] autorelease];
    args1.serviceURL=ServiceURL;
    args1.serviceNameSpace=@"http://tempuri.org/";
    args1.methodName=@"GetInfoDetail";
    args1.soapParams=params;
    
    //NSLog(@"soap=%@\n",args1.soapMessage);
    ServiceResult *result=[ServiceHelper syncService:args1];
    
    NSString *xml=[[result.request responseString] stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"];
    xml=[xml stringByReplacingOccurrencesOfString:@"&gt;" withString:@">"];
    
    //NSLog(@"xml=%@\n",xml);
    NSString *tag1=@"<Content>";
    NSString *tag2=@"</Content>";
    
    NSRange range1=[xml rangeOfString:tag1];
    if (range1.length!=0) {
        xml=[xml substringFromIndex:(range1.location+range1.length)];
        
        NSRange range2=[xml rangeOfString:tag2];
        xml=[xml substringToIndex:range2.location];
    }
    //xml=[xml stringByReplacingOccurrencesOfString:@"&amp;lt;p&amp;gt;" withString:@""];
    
    xml=[xml stringByReplacingOccurrencesOfString:@"\r\n" withString:@""];
    xml=[xml stringByReplacingOccurrencesOfString:@"&amp;lt;p&amp;gt;\t" withString:@"\r\n"];
    xml=[xml stringByReplacingOccurrencesOfString:@"&amp;lt;/p&amp;gt;" withString:@""];
    xml=[xml stringByReplacingOccurrencesOfString:@"&amp;" withString:@""];
    xml=[xml stringByReplacingOccurrencesOfString:@"lt;" withString:@""];
    xml=[xml stringByReplacingOccurrencesOfString:@"br;" withString:@""];
    xml=[xml stringByReplacingOccurrencesOfString:@"/&amp;" withString:@""];
    xml=[xml stringByReplacingOccurrencesOfString:@"amp;" withString:@""];
    xml=[xml stringByReplacingOccurrencesOfString:@"nbsp;" withString:@""];
    xml=[xml stringByReplacingOccurrencesOfString:@"br /gt;" withString:@""];
    
    {
        NSRegularExpression *regular = [[NSRegularExpression alloc] initWithPattern:@"(?<=STYLE\\>).*(?=</STYLE)" options:NSRegularExpressionCaseInsensitive error:nil];
        for (NSTextCheckingResult *checkingResult in [regular matchesInString:xml options:0 range:NSMakeRange(0, xml.length)]) {
            
            NSString *response_result = [xml substringWithRange:checkingResult.range];
            
            if (response_result.length) {
                xml = [xml stringByReplacingOccurrencesOfString: response_result withString: @""];
            }
            
        }
    }
    
    
    if (xml.length) {
        if (success) {
            success(xml);
        }
    }
    else {
        if (faile) {
            faile();
        }
    }
}


@end

@implementation KKInfoItem

@end
